var httpsUrl = getApp().globalData.httpsUrl;
var globalData = getApp().globalData;
var commonUtil = require("../../../utils/commonUtil.js")
Page({
  data: {
    imgUrl: globalData.imgUrl,
    pageNo: 1,
    pageSize: 9,
    list: [],
    totalPage: 1,//总页数
    banners:[],
  },
  list: function () {
    var params = new Object();
    params.pageNo = this.data.pageNo;
    params.pageSize = this.data.pageSize;
    var that = this;
    let openId = wx.getStorageSync("openidKey");
    console.log("param:" + JSON.stringify(params));
    wx.request({
      header: { "openId": openId, "content-Type": "application/x-www-form-urlencoded" },
      url: httpsUrl + "/poster/list",
      dataType: "json",
      method: "GET",
      data: params,
      success: function (res) {
        res = res.data;
        console.log("海报：" + JSON.stringify(res));
        if (res.result == 0) {
          let rows = res.data.list;
          that.setData({ totalPage: res.data.totalPage })
          if (that.data.pageNo == 1) {
            that.setData({ list: rows })
          } else {
            var list = that.data.list;
            list = list.concat(rows);
            that.setData({ list: list })
          }
        }
      }
    })
  },
  posterAdd:function(e){
    let index = e.currentTarget.id;
    let list = this.data.list;
    var params = new Object();
    params.code = list[index].poster_no;
    let openId = wx.getStorageSync("openidKey");
    wx.request({
      header: { "openId": openId, "content-Type": "application/x-www-form-urlencoded" },
      url: httpsUrl + "/poster/getByCode",
      dataType: "json",
      method: "GET",
      data: params,
      success: function (res) {
        res = res.data;
        console.log("详情info：" + JSON.stringify(res));
        if (res.result == 0) {
          wx.setStorageSync("posterInfo",res.data )
          wx.navigateTo({
            url: '../posterAdd/posterAdd',
          })
        }
      }
    })
  },
  //顶部大图 
  banners: function () {
    var that = this;
    var params = new Object();
    params.advSite = 4;//广告位置 "1":首页banner大图, "2":首页文字公告, "3":商家页banner大图, "4":海报页banner大图	
    let openId = wx.getStorageSync("openidKey");
    wx.request({
      header: { "openId": openId, "content-Type": "application/x-www-form-urlencoded" },
      url: httpsUrl + "/adver/getPosAdverList",
      dataType: "json",
      method: "GET",
      data: params,
      success: function (res) {
        res = res.data;
        console.log("大图：" + JSON.stringify(res));
        if (res.result == 0) {
          that.setData({ banners: res.data })
        }
      }
    })
  },
  //广告跳转 link_type：1,链接到商家首页 2,链接到商品页  link_data：参数code
  bannerGo: function (e) {
    let id = e.currentTarget.id;
    let banners = this.data.banners;
    let banner = banners[id];
    if (banner.link_type == 1) {
      wx.setStorageSync("businessCode", banner.link_data);
      wx.navigateTo({
        url: '../../shop/shopInfo/shopInfo',
      })
    } else if (banner.link_type == 2) {
      wx.setStorageSync("goodsCode", banner.link_data);
      wx.navigateTo({ url: '../../shop/goodsInfo/goodsInfo', })
    }
  },
  content: function () {
    wx.setStorageSync("contentsType", 4);
    wx.navigateTo({
      url: '../contents/contents',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.list();
    this.banners();
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
 
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let pageNo = this.data.pageNo;
    pageNo = pageNo + 1;
    if (pageNo > this.data.totalPage) {
      commonUtil.showSuccessToast("没有更多啦~");
      return;
    } else {
      this.setData({ totalPage: totalPage })
      this.list();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})